package cn.tannn.cat.file.server.controller.manage.dto;

import cn.tannn.cat.file.sdk.config.Aws3Config;
import cn.tannn.cat.file.sdk.config.LocalConfig;
import cn.tannn.cat.file.sdk.config.MinioConfig;
import cn.tannn.cat.file.sdk.config.QiNiuConfig;
import cn.tannn.cat.file.sdk.enums.StorageMaster;
import cn.tannn.cat.file.server.entity.FileStorageConfig;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 新增存储器配置元数据 config-new.json
 *
 * @author tnnn
 * @version V1.0
 * @date 2024-03-09 20:56
 */
@Schema(description = "新增存储器配置元数据config-new.json")
@ToString
@Getter
@Setter
public class StorageConfigAppend2 {
    /**
     * 配置名
     */
    @Schema(description ="配置名")
    @NotBlank
    private String name;

    /**
     * 字典值[Dict.value]
     */
    @Schema(description ="字典值[Dict.value]")
    @NotBlank
    private String storage;


    /**
     * 存储配置Json
     * @see Aws3Config
     * @see LocalConfig
     * @see MinioConfig
     * @see QiNiuConfig
     */
    @Schema(description ="存储配置Json[根据存储器配置不同的json对象]")
    @NotEmpty
    private JSONArray config;


    /**
     * 备注
     */
    @Schema(description ="备注")
    private String remark;

    public FileStorageConfig toFileConfig() {
        FileStorageConfig fileConfig = new FileStorageConfig();
        fileConfig.setName(this.name);
        fileConfig.setStorage(this.storage);
        fileConfig.setMaster(StorageMaster.SLAVE);
        JSONObject firstObj = this.config.getJSONObject(0);
        firstObj.remove("type");
        firstObj.remove("name");
        firstObj.remove("label");
        fileConfig.setConfig(firstObj);
        fileConfig.setRemark(this.remark);
        return fileConfig;
    }
}
